package com.wintop.youyuan.ajax;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.wintop.youyuan.model.BaseData;
import com.wintop.youyuan.model.DetailData;
import com.wintop.youyuan.model.PersonalsCondition;
import com.wintop.youyuan.model.Photo;
import com.wintop.youyuan.model.User;
import com.wintop.youyuan.services.UserInformationServices;
import com.wintop.youyuan.services.UserServices;
import com.wintop.youyuan.util.MD5Util;

@Scope("prototype")
@Controller
public class UserAjax extends ActionSupport implements RequestAware,
		SessionAware, ApplicationAware, ServletResponseAware {
	private static final long serialVersionUID = 1L;
	private Map<String, Object> request;
	private Map<String, Object> session;
	private Map<String, Object> application;
	private HttpServletResponse response;
	private User user;
	private UserServices userServices;
	private UserInformationServices userInformationServices;
	private BaseData baseData;
	private DetailData detailData;
	private PersonalsCondition personalsCondition;
	private int age;
	private File image;
	private String imageFileName;
	private HashMap<String, Object> otherData;// 临时数据储存

	private String securityCode;// 验证码

	/*
	 * 检查验证码
	 */
	public void checkSecurityCode() {
		String s = (String) session.get("rand");
		boolean isHave = s.equals(securityCode);
		printString(isHave);
	}

	/*
	 * 登录
	 */
	public void login() {
		User userInfo = userServices
				.login(user.getUserid(), user.getPassword());
		if (userInfo != null) {
			session.put("userInfo", userInfo);
			System.out.println(userInfo);
			if (userInfo.getVip() == 2) {
				printString("trueVip");
			} else {
				printString(true);
			}
		} else {
			printString(false);
		}
	}

	/*
	 * 注册
	 */
	public void register() {
		User userInfo = userServices.register(user, age);
		boolean isLogin = false;
		if (userInfo != null) {
			session.put("userInfo", userInfo);
			isLogin = true;
		}
		printString(isLogin);
	}

	/*
	 * 检查用户名是否存在
	 */
	public void checkIsHaveUser() {
		boolean ishave = userServices.isHaveUser(user.getUserid());
		printString(ishave);
	}

	/*
	 * 检查老密码是否正确
	 */
	public void checkOldPassword() {
		User userInfo = (User) session.get("userInfo");
		boolean ishave = userInfo.getPassword().equals(
				MD5Util.md5Encode(user.getPassword()));
		printString(ishave);
	}

	/*
	 * 修改密码
	 */
	public void changePassword() {
		User userInfo = (User) session.get("userInfo");
		boolean ishave = userServices.updatePwd(userInfo, user.getPassword());
		printString(ishave);
	}

	/*
	 * 删除用户
	 */
	public void deleteUser() {
		boolean ishave = userServices.deleteUser(user.getId());
		printString(ishave);
	}

	/*
	 * 更新基本信息
	 */
	public void updateBaseData() {
		User userInfo = (User) session.get("userInfo");
		userInfo.setName(user.getName());
		userServices.updateUser(userInfo);
		baseData.setUserid(userInfo.getUserid());
		boolean ishave = userInformationServices.updateBaseData(baseData);
		printString(ishave);
	}

	/*
	 * 更新详细资料
	 */
	public void updateDetailData() {
		User userInfo = (User) session.get("userInfo");
		detailData.setUserid(userInfo.getUserid());
		boolean ishave = userInformationServices.updateDetailData(detailData);
		printString(ishave);
	}

	/*
	 * 更新内容独白
	 */
	public void updateSoliloquy() {
		User userInfo = (User) session.get("userInfo");
		boolean ishave = userInformationServices.updateSoliloquy(
				userInfo.getUserid(), detailData.getSoliloquy());
		printString(ishave);
	}

	/*
	 * 上传头像
	 */
	public void upIcon() {
		User userInfo = (User) session.get("userInfo");
		String realpath = ServletActionContext.getServletContext().getRealPath(
				"/images/userImages/" + userInfo.getUserid());
		imageFileName = "xf" + (new Date().getTime())
				+ imageFileName.substring(imageFileName.length() - 5);
		if (image != null) {
			File savefile = new File(new File(realpath), imageFileName);
			if (!savefile.getParentFile().exists())
				savefile.getParentFile().mkdirs();
			try {
				FileUtils.copyFile(image, savefile);
			} catch (IOException e) {
				printString(false);
				e.printStackTrace();
			}
		}
		session.put("tempImageName", imageFileName);
		String fm = "images\\userImages\\" + userInfo.getUserid() + "\\" + imageFileName;
		printString(fm);
	}

	/*
	 * 保存头像
	 */
	public void saveIcon() {
		User userInfo = (User) session.get("userInfo");
		String fileName = (String) session.get("tempImageName");
		System.out.println(fileName);
		boolean ishave = userInformationServices.updateIcon(
				userInfo.getUserid(), fileName);
		printString(ishave);
	}

	/*
	 * 更新交友信息
	 */
	public void updatePersonalsCondition() {
		User userInfo = (User) session.get("userInfo");
		personalsCondition.setUserid(userInfo.getUserid());
		boolean ishave = userInformationServices
				.updatePersonalsCondition(personalsCondition);
		printString(ishave);
	}

	/*
	 * 添加相片
	 */
	public void addPhoto() {
		User userInfo = (User) session.get("userInfo");
		String realpath = ServletActionContext.getServletContext().getRealPath(
				"/images/userImages/" + userInfo.getUserid());
		imageFileName = "xf" + (new Date().getTime())
				+ imageFileName.substring(imageFileName.length() - 5);
		if (image != null) {
			File savefile = new File(new File(realpath), imageFileName);
			if (!savefile.getParentFile().exists())
				savefile.getParentFile().mkdirs();
			try {
				FileUtils.copyFile(image, savefile);
			} catch (IOException e) {
				printString(false);
				e.printStackTrace();
			}
		}
		Photo photo = new Photo();
		photo.setUserid(userInfo.getUserid());
		photo.setSrc(imageFileName);
		boolean ishave = userInformationServices.addPhoto(photo);
		printString(ishave);
	}

	/*
	 * 删除图片
	 */
	public void deletePhoto() {
		String id = (String) otherData.get("id");
		boolean ishave = userInformationServices.deletePhoto(Integer
				.parseInt(id));
		printString(ishave);
	}

	/*
	 * 向页面输出
	 */
	private void printString(Object str) {
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		out.print(str);
		out.close();
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public BaseData getBaseData() {
		return baseData;
	}

	public void setBaseData(BaseData baseData) {
		this.baseData = baseData;
	}

	public DetailData getDetailData() {
		return detailData;
	}

	public void setDetailData(DetailData detailData) {
		this.detailData = detailData;
	}

	public PersonalsCondition getPersonalsCondition() {
		return personalsCondition;
	}

	public void setPersonalsCondition(PersonalsCondition personalsCondition) {
		this.personalsCondition = personalsCondition;
	}

	public String getSecurityCode() {
		return securityCode;
	}

	public void setSecurityCode(String securityCode) {
		this.securityCode = securityCode;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Map<String, Object> getRequest() {
		return request;
	}

	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public Map<String, Object> getApplication() {
		return application;
	}

	public void setApplication(Map<String, Object> application) {
		this.application = application;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	@Resource(name = "userServices")
	public void setUserServices(UserServices userServices) {
		this.userServices = userServices;
	}

	@Resource(name = "userInformationServices")
	public void setUserInformationServices(
			UserInformationServices userInformationServices) {
		this.userInformationServices = userInformationServices;
	}

	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public HashMap<String, Object> getOtherData() {
		return otherData;
	}

	public void setOtherData(HashMap<String, Object> otherData) {
		this.otherData = otherData;
	}

}
